﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AssetLoader 
{
    public AssetLoader() { }
    public AssetLoader(AssetBundle asset) {
        assetBundle=asset;
    }
    private AssetBundle asset;
    public AssetBundle assetBundle { set { asset = value; } get { return asset; } }
    /// <summary>
    /// 通过资源名称获取一个资源
    /// </summary>
    /// <param name="assetName"></param>
    /// <returns></returns>
    public Object LoadAsset(string assetName)
    {
        if(asset == null || !asset.Contains(assetName))
        {
            Debug.LogError("当前资源包没有找到【"+assetName+"】资源");
            return null;
        }
        return asset.LoadAsset(assetName);
    }
    public T LoadAsset<T>(string assetName)where T : Object
    {
        if (asset == null || !asset.Contains(assetName))
        {
            Debug.LogError("当前资源包没有找到【" + assetName + "】资源");
            return null;
        }
        return asset.LoadAsset<T>(assetName);
    }

    public T[] LoadAssetAll<T>() where T : Object
    {
        return asset?.LoadAllAssets<T>();
    }
    /// <summary>
    /// 获取图集资源的全部子集
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="assetName"></param>
    /// <returns></returns>
    public UnityEngine.Object[] LoadAssetWithSubAssets(string assetName)
    {
        return asset?.LoadAssetWithSubAssets(assetName);
    }
    /// <summary>
    /// 卸载资源
    /// </summary>
    /// <param name="asset"></param>
    public void UnLoadAsset(Object asset)
    {
        Resources.UnloadAsset(asset);
    }
    /// <summary>
    /// 释放assetbundle资源
    /// </summary>
    public void Dispose()
    {
        asset?.Unload(false);
        asset = null;
    }
    /// <summary>
    /// 测试专用，获取当前assetbundle资源包的已经加载好的全部资源名称
    /// </summary>
    /// <returns></returns>
    public string[] GetAllAssetNames()
    {
        return asset?.GetAllAssetNames();
    }
    public void Trace()
    {
        string[] all = GetAllAssetNames();
        for (int i = 0; i < all.Length; i++) UnityEngine.Debug.Log("资源 >> " + all[i]);
    }
}
